package com.qiangqiang.resoldhouse.controller;

import com.qiangqiang.resoldhouse.dto.AppointmentDTO;
import com.qiangqiang.resoldhouse.dto.PageDTO;
import com.qiangqiang.resoldhouse.entity.Appointment;
import com.qiangqiang.resoldhouse.entity.AppointmentSelect;
import com.qiangqiang.resoldhouse.entity.User;
import com.qiangqiang.resoldhouse.service.AppointmentService;
import com.qiangqiang.resoldhouse.service.UserService;
import com.qiangqiang.resoldhouse.util.TimeFormat;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zqh
 * @date: 2018/8/14 10:31
 * @description:
 */
@Controller
public class AppointmentController {

    @Autowired
    private AppointmentService appointmentService;

    @Autowired
    private UserService userService;

    @RequestMapping("/add_appointment")
    public String addAppointment(Map<String, Object> map, HttpSession session, Integer year, Integer month, Integer day, Appointment appointment){
        User user = (User)session.getAttribute("user");
        if(user == null) return "redirect:/login_user";

        appointment.setOrderTime(new Date(year,month,day).getTime() -
                new Date(1970,1,0).getTime());
        appointment.setUserId(user.getUserId());
        boolean result = appointmentService.addAppointment(appointment);
        return "redirect:user/user_appointment_list";
    }

    //用户个人中心查看自己的预约，可选择已完成，未完成，以评价，未评价
    //经纪人个人中心查看自己的预约，能看到所有历史预约，按时间排序，
    //参数 Integer page 表示页数，默认为1，Long agentId 表示要查看的经纪人的Id，Long userId表示查看的用户的id
    @ResponseBody
    @RequestMapping("/ajax_appointment_list")
    public Map getAgentAppointmentList(@RequestParam(defaultValue = "1") Integer page, AppointmentSelect select){
        Map<String, Object> map = new HashMap<>();
        PageDTO<AppointmentDTO> pageDTO = appointmentService.findPage(page, select);
        map.put("result", pageDTO.getResult());
        map.put("current", pageDTO.getPage().getCurrent());
        map.put("pages", pageDTO.getPage().getPages());
        map.put("hasPrePage", pageDTO.getPage().hasPrevious());
        map.put("hasNextPage", pageDTO.getPage().hasNext());
        map.put("total", pageDTO.getPage().getTotal());
        return map;
    }
}
